using DDA;
using QFramework;
using UnityEngine;

namespace Survivor
{
    public class Global:Architecture<Global>
    {
        public static BindableProperty<int> Hp = new(3);
        public static BindableProperty<int> MaxHp = new(3);
        public static BindableProperty<int> Exp = new(0);
        public static BindableProperty<int> Coin = new(0);
        public static BindableProperty<int> ExpGainValue = new(1);  // 经验获取量提升数值
        public static BindableProperty<int> CoinGainValue = new(1);  // 金币获取量提升数值

        public static BindableProperty<int> Level = new(0);

        public static BindableProperty<float> CurrentTime = new(0);

        public static BindableProperty<string> DDALog = new("");

        #region 普通攻击
        public static BindableProperty<bool> SimpleAttackUnlock = new(false);
        public static BindableProperty<int> SimpleAttackDamage = new(Config.InitSimpleAttackDamage);
        public static BindableProperty<float> SimpleAttackInterval = new(Config.InitSimpleAttackInterval);
        public static BindableProperty<float> SimpleAttackRange = new(Config.InitSimpleAttackRange);
        public static BindableProperty<int> SimpleAttackCount = new(Config.InitSimpleAttackCount);
        #endregion

        #region 环绕攻击
        public static BindableProperty<bool> SimpleCircleUnlock = new(false);
        public static BindableProperty<int> SimpleCircleDamage = new(Config.InitSimpleCircleDamage);
        public static BindableProperty<int> SimpleCircleSpinSpeed = new(Config.InitSimpleCircleSpinSpeed);
        public static BindableProperty<float> SimpleCircleScale = new(Config.InitSimpleCircleScale);
        public static BindableProperty<int> SimpleCircleCount = new(Config.InitSimpleCircleCount);
        #endregion


        #region  小刀攻击
        public static BindableProperty<bool> SimpleKnifeUnlock = new(false);
        public static BindableProperty<int> SimpleKnifeDamage = new(Config.InitSimpleKnifeDamage);
        public static BindableProperty<float> SimpleKnifeInterval = new(Config.InitSimpleKnifeInterval);
        public static BindableProperty<int> SimpleKnifeCount = new(Config.InitSimpleKnifeCount);
        #endregion

        #region 斧头攻击
        public static BindableProperty<bool> SimpleAxeUnlock = new(false);
        public static BindableProperty<int> SimpleAxeDamage = new(Config.InitSimpleAxeDamage);
        public static BindableProperty<float> SimpleAxeInterval = new(Config.InitSimpleAxeInterval);
        public static BindableProperty<float> SimpleAxeScale = new(Config.InitSimpleAxeScale);
        public static BindableProperty<int> SimpleAxeCount = new(Config.InitSimpleAxeCount);
        #endregion

        #region 被动属性
        public static BindableProperty<float> CriticalRate = new(Config.InitCriticalRate);
        public static BindableProperty<float> DamageRate = new(Config.InitDamageRate);
        public static BindableProperty<int> FlyThingCount = new(Config.InitFlyThingCount);
        public static BindableProperty<float> MoveSpeedRate = new(Config.InitMoveSpeedRate);
        public static BindableProperty<float> PickUpRangeRate = new(Config.InitPickUpRangeRate);
        public static BindableProperty<float> ExpRate = new(Config.InitExpRate);
        
        #endregion

        
        
        public static readonly int GameWinAliveTime = 999;

        [RuntimeInitializeOnLoadMethod]
        public static void AutoInit()
        {
            UIKit.Root.SetResolution(1920,1080,1);
            
            Coin.Value = PlayerPrefs.GetInt(nameof(Coin), 0);
            CoinGainValue.Value = PlayerPrefs.GetInt(nameof(CoinGainValue), 1);
            ExpGainValue.Value = PlayerPrefs.GetInt(nameof(ExpGainValue), 1);
            MaxHp.Value = PlayerPrefs.GetInt(nameof(MaxHp), 3);
            
            //Test 
            MaxHp.Value = 999999;

            Coin.Register((value) =>
            {
                PlayerPrefs.SetInt(nameof(Coin), value);
            });
            CoinGainValue.Register((value) =>
            {
                PlayerPrefs.SetInt(nameof(CoinGainValue), value);
            });
            ExpGainValue.Register((value) =>
            {
                PlayerPrefs.SetInt(nameof(ExpGainValue), value);
            });
            MaxHp.Register((value) =>
            {
                PlayerPrefs.SetInt(nameof(MaxHp),value);
            });
            
            Hp.Value = MaxHp.Value;
        }

        public static void ResetData()
        {
            Player.Default = null;
            Hp.Value = MaxHp.Value;
            Exp.Value = 0;
            Level.Value = 0;
            CurrentTime.Value = 0;
            
            SimpleAttackDamage.Value = Config.InitSimpleAttackDamage;
            SimpleAttackInterval.Value = Config.InitSimpleAttackInterval;
            SimpleAttackRange.Value = Config.InitSimpleAttackRange;
            SimpleAttackCount.Value = Config.InitSimpleAttackCount;
            
            SimpleCircleDamage.Value = Config.InitSimpleCircleDamage;
            SimpleCircleSpinSpeed.Value = Config.InitSimpleCircleSpinSpeed;
            SimpleCircleScale.Value = Config.InitSimpleCircleScale;
            SimpleCircleCount.Value = Config.InitSimpleCircleCount;

            SimpleKnifeDamage.Value = Config.InitSimpleKnifeDamage;
            SimpleKnifeInterval.Value = Config.InitSimpleAttackInterval;
            SimpleKnifeCount.Value = Config.InitSimpleKnifeCount;
            
            SimpleAxeDamage.Value = Config.InitSimpleAxeDamage;
            SimpleAxeScale.Value = Config.InitSimpleAxeScale;
            SimpleAxeInterval.Value = Config.InitSimpleAxeInterval;
            SimpleAxeCount.Value = Config.InitSimpleAxeCount;

            CriticalRate.Value = Config.InitCriticalRate;
            DamageRate.Value = Config.InitDamageRate;
            FlyThingCount.Value = Config.InitFlyThingCount;
            MoveSpeedRate.Value = Config.InitMoveSpeedRate;
            PickUpRangeRate.Value = Config.InitPickUpRangeRate;
            ExpRate.Value = Config.InitExpRate;

            SimpleAttackUnlock.Value = false;
            SimpleAxeUnlock.Value = false;
            SimpleKnifeUnlock.Value = false;
            SimpleCircleUnlock.Value = false;

            EnemyGenerator.EnemyNum.Value = 0;
            
            DDAParser.Instance.Stop();

            DDALog.Value = "";
            
            Global.Interface.GetSystem<ExpUpgradeSystem>().Reset();
            
        }

        public static int ExpToNextLevel()
        {
            return Mathf.FloorToInt(0.5f * Level.Value * Level.Value+1f) * 10;
        }

        public static void GainExp()
        {
            Exp.Value += Mathf.RoundToInt(ExpGainValue.Value * ExpRate.Value);
            if (Exp.Value >= ExpToNextLevel())
            {
                int needSubExp = ExpToNextLevel();
                Level.Value += 1; // 先加等级再更新经验，这样经验文本才会显示正确的下一级所需经验
                Exp.Value -= needSubExp;
            }
        }

        public static void GainCoin()
        {
            Coin.Value += CoinGainValue.Value;
        }

        protected override void Init()
        {
            RegisterSystem(new CoinUpgradeSystem());
            RegisterSystem(new SaveSystem());
            RegisterSystem(new ExpUpgradeSystem());
        }
        
    }
}






















